package 锁.lock.fairLock;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/9/5 15:42
 * @description:
 * @version:
 */
public class FairLock {
    //公平锁和非公平锁
    Lock lock = new ReentrantLock(false);
    public void doSth(){
        lock.lock();
        Random random = new Random();
        long randomNumber = (long) (random.nextDouble() * 501) + 500;
        try {
            Thread.sleep(randomNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "获的锁子");
        lock.unlock();
    }
}
